import sys
import pylab as plt
import os
#import plot_settings
from params import *
import random

#plot_settings.set_mode('publish_square')

names = {'SOCIAL':'Social', 'PLACE':'Place','BOTH':'Place-social'}

def average_plots(plots):
    return plots[0]
    num_bins = 100
    d = [ [] for i in range(num_bins+1)]
    for x,y in plots:
        for a,b in zip(x,y):
            index = int(1.0*a*num_bins)
            d[index].append(b)

    tot_x,tot_y = [],[]
    for i in range(num_bins+1):
        if len(d[i])>0:
            tot_x.append(1.0*i/num_bins)
            tot_y.append(sum(d[i])/len(d[i]))

    x = [tot_x[0]]
    for i in range(1,len(tot_x)):
        x.append(max(x[i-1],tot_x[i]))

    y = [tot_y[0]]
    for i in range(1,len(tot_y)):
        y.append(max(y[i-1],tot_y[i]))

    if x[0] > 0 or y[0] > 0:
        x.insert(0,0.0)
        y.insert(0,0.0)
    return x,y

def avg(data,name):
    r = data[name]
    N = len(r)
    if N:
        return 1.0*sum(r)/N
    else:
        return None


DIR = os.path.join(WORKDIR, 'gowalla', 'final', 'kdd_last')
plots = {}
for file in os.listdir(DIR):
    name,ext = os.path.splitext(file)
    if not name.startswith('roc'):
        continue
    t = name.split('_')

    print file
    month, flag,it,algo = t[1:]
#    if algo in set(('logistic','regression','svmrbf','svmpoly')):
#        continue
#    if algo != 'regression':
#        continue
    month, it = map(int,(month,it))
    if it > 3:
        continue
    if month != 5:
        continue
    print algo
    if algo in set(('regression','randomforest')):
    #if algo in set(('randomforest')):
        print month, it, flag
        f = open(os.path.join(DIR,file))
        headers = f.readline().strip().split(',')
        data = dict((h,[]) for h in headers)
        for line in f:
            try:
                values = map(float,line.strip().split(','))
            except ValueError:
                continue
            for h,x in zip(headers,values):
                data[h].append(x)
        #x = data['False Negative Rate']
        #y = data['True Negative Rate']
        #x.reverse()
        #y.reverse()

        
        #inverting positive with negatives
        #since WEKA uses as positive the majority class
        #which in this case is NEGATIVE ITEMS (no link)
        tp_values = data['True Negatives']
        tn_values = data['True Positives']
        fp_values = data['False Negatives']
        fn_values = data['False Positives']
        prec = []
        rec = []
        tpr = []
        fpr = []

#        for tp,tn,fp,fn in zip(tp_values,tn_values,fp_values,fn_values):
#            if tp:
#                tpr.append(float(tp)/(tp+fn))
#                fpr.append(float(fp)/(fp+tn))
#                print fpr[-1], tpr[-1]
#        x = fpr
#        y = tpr
#
        data = []
        data = {}
        m =  0
        for tp,fp,fn in zip(tp_values,fp_values,fn_values):
            if tp:
                p = float(tp)/(tp+fp)
                m = max(p,m) 

        print 'max ', m
        flow = False
        for tp,fp,fn in zip(tp_values,fp_values,fn_values):
            #print 'TP %d, FP %d, FP %d'%(tp,fp,fn)
            if tp:
                p = float(tp)/(tp+fp)
                if p == m:
                    flow = True
                p = round(p,2)
                if not flow:
                    continue
                r = float(tp)/(tp+fn)
                #print 'prec %f, rec %f'%(p,r)
                data.setdefault(p,[]).append(r)
                #data.append((p,r))
                #if not data:
                #    data.append((p,r))
                #elif p < data[-1][0]:
                #    data.append((p,r))
        x = sorted(data)
        y = [max(data[k]) for k in x]
        #x = x[100:]
        #y = y[100:]
        print x
        print y

#        print data2[-5:]
##        if len(data) > 500:
##            data2 = sorted(random.sample(data,500))
#        #data2.insert(0,(0,1))
#        #data2.append((1,0))
#
##        prec = [float(a)/(a+b) for a,b in zip(tp,fp)]
##        rec = [float(a)/(a+b) for a,b in zip(tp,fn)]
#        x = [a for a,b in data]
#        y = [b for a,b in data]

        #TPR vs FPR
        plots.setdefault(algo,{}).setdefault(month,{}).setdefault(flag,{})[it] = (x,y)

for algo in ['regression','randomforest']:#plots:
    print algo
    FIG_AXES2 = [0.18,0.2,0.95-0.18,0.95-0.2]
    for month in plots[algo]:
        print month
        plt.figure()
        plt.clf()
        plt.axes(FIG_AXES2)
        i = 0
        lbl = []
        markers = list('x+.svo')
        markers = ['-.','--',':']
        for flag,m in zip(sorted(plots[algo][month],reverse=True),markers):
            lbl.append(names[flag])
            all_plots = []
            for it in plots[algo][month][flag]:
                x,y = plots[algo][month][flag][it]
                all_plots.append((x,y))
            tot_x,tot_y = average_plots(all_plots)
            plt.plot(tot_x,tot_y,'k%s'%m,linewidth=2)
        plt.legend(lbl,loc='lower left',numpoints=1,
                prop={'size':10})
        #plt.plot(tot_x,tot_x,'k-')
        plt.axis((0,1,0,1))
        plt.xlabel('Precision')
        plt.ylabel('Recall')
        plt.grid(True)
        plt.savefig('test_precall_%d_%s.pdf'%(month,algo))
        plt.close()

sys.exit()

for month in [5,6,7]:
    plt.figure()
    plt.clf()
    FIG_AXES2 = [0.18,0.2,0.95-0.18,0.95-0.2]
    plt.axes(FIG_AXES2)
    algo = 'regression'
    markers = 'x+.'
    lbl = []
    for flag,m in zip(sorted(plots[algo][month],reverse=True),markers):
        lbl.append(names[flag])
        all_plots = []
        for it in plots[algo][month][flag]:
            x,y = plots[algo][month][flag][it]
            all_plots.append((x,y))
        tot_x,tot_y = average_plots(all_plots)
        plt.plot(tot_x,tot_y,'k%s'%m,linewidth=1)
    algo = 'randomforest'
    for flag,m in zip(sorted(plots[algo][month],reverse=True),markers):
        all_plots = []
        for it in plots[algo][month][flag]:
            x,y = plots[algo][month][flag][it]
            all_plots.append((x,y))
        tot_x,tot_y = average_plots(all_plots)
        plt.plot(tot_x,tot_y,'k%s-'%m,linewidth=1)

    plt.legend(lbl,loc='lower right',numpoints=1,
            prop={'size':10})
    plt.plot(tot_x,tot_x,'k-')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.grid(True)
    plt.savefig('test_rocplot_%d_double.pdf'%(month))
    plt.close()

for algo in ['regression','randomforest']:#plots:
    print algo
    FIG_AXES2 = [0.18,0.2,0.95-0.18,0.95-0.2]
    markers = list('x+.svo')
    for month in plots[algo]:
        print month
        plt.figure()
        plt.clf()
        plt.axes(FIG_AXES2)
        i = 0
        lbl = []
        for flag,m in zip(sorted(plots[algo][month],reverse=True),markers):
            lbl.append(names[flag])
            all_plots = []
            for it in plots[algo][month][flag]:
                x,y = plots[algo][month][flag][it]
                all_plots.append((x,y))
            tot_x,tot_y = average_plots(all_plots)
            plt.plot(tot_x,tot_y,'k%s-'%m,linewidth=1)
        plt.legend(lbl,loc='lower right',numpoints=1,
                prop={'size':10})
        plt.plot(tot_x,tot_x,'k-')
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.grid(True)
        plt.savefig('test_rocplot_%d_%s.pdf'%(month,algo))
        plt.close()

